package com.yonyou.ucf.mdf.app.isvdxq.firstpart.stock.controller;

import com.yonyou.ucf.mdf.app.isvdxq.ReturnResult.DataResult;
import com.yonyou.ucf.mdf.app.isvdxq.common.DxqConfig;
import com.yonyou.ucf.mdf.app.isvdxq.firstpart.guize.mapper.GuiZeMapperImpl;
import com.yonyou.ucf.mdf.app.isvdxq.firstpart.stock.model.CoderfidBean;
import com.yonyou.ucf.mdf.app.isvdxq.firstpart.stock.service.StockService;
import com.yonyou.ucf.mdf.app.isvdxq.other.entity.WarehouseEntity;
import com.yonyou.ucf.mdf.app.isvdxq.other.service.BaseService;
import com.yonyou.ucf.mdf.app.isvdxq.other.service.MyTokenService;
import com.yonyou.ucf.mdf.app.isvdxq.other.service.OrgUnitService;
import com.yonyou.ucf.mdf.app.isvdxq.other.service.WarehouseService;
import com.yonyou.ucf.mdf.app.isvdxq.secondpart.service.TaglistService;
import com.yonyou.ucf.mdf.app.isvdxq.utils.RequestTool;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping(value = {DxqConfig.pre_apiUrl+"/stock",DxqConfig.pre_pcUrl+"/stock"})
public class StockController {

    @Autowired
    StockService stockService;
    @Autowired
    BaseService baseService;

    @Autowired
    MyTokenService myTokenService;

    @Autowired
    OrgUnitService orgUnitService;
    @Autowired
    WarehouseService warehouseService;
    @Autowired
    TaglistService taglistService;
    @Autowired
    GuiZeMapperImpl guiZeMapper;

    /**
     * 根据RfidCode 获取绑定的货品信息
     *
     * @return 返回货品列表
     */
    @RequestMapping("/GetGoodsByLabelCode")
    public DataResult GetGoodsByLabelCode(HttpServletRequest request, String warehouseIds, String orderId, String ordertype, String RfidCode) {
        DataResult dataResult = new DataResult(0, "标签未被使用");
        try {
            List<String> epcList = new ArrayList<>();
            String tenantId = baseService.getTenantId(request);
            String userId = RequestTool.getHeaderAndParameter(request, DxqConfig.DXQ_userId);
            Map<String, String> reqParams = RequestTool.getParams(request);
            List<String> warehouseIds1 = baseService.getWarehouseIds(userId, tenantId);
            List<String> warehouseIdsArray = warehouseIds1;
            if (!StringUtils.isEmpty(warehouseIds)){
                warehouseIdsArray = Arrays.asList(warehouseIds.split(","));
            }
            if (StringUtils.isNotEmpty(RfidCode)) {
                epcList = Arrays.asList(RfidCode.split(","));
            }
            if (epcList.size() == 0) {
                return dataResult;
            }
            if (StringUtils.isEmpty(orderId)) {
                dataResult = stockService.GetGoodsByLabelCode(epcList, "", "", warehouseIdsArray, tenantId);
            }
            else {
                dataResult = stockService.GetGoodsByLabelCode(epcList, orderId, ordertype, warehouseIdsArray, tenantId);
            }
        } catch (Exception ex) {
            dataResult.setMessage(ex.getMessage());
        }
        return dataResult;
    }

    /**
     * 根据标签和分页信息返回结果
     *
     * @param rfidCode 标签值 有可能为货位也有可能为货品
     * @param page     页面
     * @param pageSize 页面大小
     * @return 返回查询结果
     */
    @RequestMapping("/GetGoodsByLabelCodeAndPage")
    public DataResult GetGoodsByLabelCodeAndPage(@RequestParam(defaultValue = "", required = false) String warhouseId
            , String rfidCode, String productName, Integer page, Integer pageSize, HttpServletRequest request) throws IOException {
        DataResult dataResult = new DataResult(1, "查找成功");
        try {
            String tenantId = baseService.getTenantId(request);
            String userId = RequestTool.getHeaderAndParameter(request, DxqConfig.DXQ_userId);
            Map<String, Object> myParams = RequestTool.getParams(request);
            if (StringUtils.isEmpty(userId)) {
                dataResult = new DataResult(0, "用户身份不能为空");
                return dataResult;
            }
            List<String> warhouseIds = new ArrayList<>();

            // 如果没有传入仓库Id的时候，进行组织机构的Id 筛选传入
            if (warhouseId == null || warhouseId.length() <= 0) {
                // 获取有权限的仓库
                List<String> orgList = baseService.getOrgListByUserId(userId, tenantId);
                if (orgList.size() == 0) {
                    dataResult = new DataResult(0, "组织权限异常");
                    return dataResult;
                }
                List<WarehouseEntity> warehouseList = warehouseService.SelectByOrgIds(orgList, tenantId);
                for (WarehouseEntity item : warehouseList
                ) {
                    warhouseIds.add(item.getId().toString());
                }
            } else {
                String[] warehouseIdsArray = warhouseId.split(",");
                warhouseIds = Arrays.asList(warehouseIdsArray);
            }

            if (pageSize == null || pageSize == 0) {
                pageSize = 10;
            }
            if (page == null || page == 0) {
                page = 1;
            }
            int pageBegin = (page - 1) * pageSize;

            dataResult = stockService.GetGoodsByLabelCodeAndPage(warhouseIds, rfidCode, productName, pageBegin, pageSize, tenantId);
//            List<productInfo> dataList = (List<productInfo>) dataResult.getDataList();
//            dataResult.setDataList(dataList);
            int totalPage = (dataResult.getTotalSize() + pageSize - 1) / pageSize;
            dataResult.setTotalPages(totalPage);
            dataResult.setPageNum(page);
            dataResult.setPageSize(pageSize);
        } catch (Exception ex) {
            dataResult.setMessage(ex.getMessage());
        }
        return dataResult;
    }

    /**
     * 拆签
     * 一期拆签 暂未使用
     *
     * @param rfidCode
     * @param tempCode
     * @param count
     * @return
     */
    //@RequestMapping("/LabelCodeSplit")
    public DataResult LabelCodeSplit(String tenant_id, String rfidCode, String tempCode, Double count) {
        DataResult dataResult = new DataResult(0, "操作失败");
        try {
            CoderfidBean model = new CoderfidBean();
            model.setCode(tempCode);
            model.setCount(BigDecimal.valueOf(count));
            List<CoderfidBean> entity = new ArrayList<>();
            entity.add(model);
            dataResult = stockService.LabelCodeSplit(rfidCode, entity, 0, tenant_id);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return dataResult;
    }
}
